package com.bw.gmall.api.repository;

import com.bw.gmall.api.model.TrafficStats;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class TrafficStatsRepository {
/*
* TrafficStatsRepository：整体流量统计数据访问类，提供查询整体流量数据的方法，
* 操作表dws_traffic_overview_window。
* */
    @Autowired
    private JdbcTemplate jdbcTemplate;

    public List<TrafficStats> findAll() {
        String sql = "SELECT * FROM dws_traffic_overview_window ORDER BY stats_time DESC LIMIT 100";
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(TrafficStats.class));
    }

    public List<TrafficStats> findByTimeRange(String startTime, String endTime) {
        String sql = "SELECT * FROM dws_traffic_overview_window WHERE stats_time BETWEEN ? AND ? ORDER BY stats_time";
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(TrafficStats.class), startTime, endTime);
    }
}